package com.sduept.nwld.dataserver.controller.document;

import com.sduept.bigdata.document.entity.core.DocumentKeyWordSearchHeat;
import com.sduept.bigdata.document.services.DocumentKeyWordSearch;
import com.sduept.core.dao.SuperDAO;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Sduept_leo
 *
 */
@Named
@ViewScoped
public class DocumentSearchController extends AbstractController implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8878652689288124180L;
	@Autowired
	DocumentKeyWordSearch manager;
	@Autowired
	private SuperDAO dao;
	
	private String keyword;
	private String info;
	private String moreInfo;
	private int preIndex;
	private int AftIndex;
	private String docName;
	public void totalKeyWord(){
		try {
			if(keyword!=null && !"".equals(keyword) ){
				info = manager.getNeedContent(keyword);
			}
			moreInfo="";
			if(StringUtils.isEmpty(info))
				info = "没有检索到该关键字所对应的信息！";
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void loadMoreInfo(){
		try {
			docName = getParameter("docName");
			String lineNum = getParameter("lineNum");
			if(docName!=null && lineNum!=null){
				preIndex = Integer.parseInt(lineNum)-6;
				AftIndex = Integer.parseInt(lineNum)+6;
				moreInfo = manager.getMoreContent(Integer.parseInt(lineNum),docName);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	//热词加载
	public List<String> completeKeyWord(String query) {
		List<String> results = new ArrayList<String>();
		List<DocumentKeyWordSearchHeat> list = dao.findAll(DocumentKeyWordSearchHeat.class, " order by searchTimes desc");
		for(DocumentKeyWordSearchHeat key:list){
			if(key.getKeyword().contains(query)){
				results.add(key.getKeyword());
			}
		}
		return results;
	}
	public void loadAftInfo(){
		try {
			String aftContent = manager.getAftContent(5, AftIndex, docName);
			moreInfo = moreInfo+aftContent;
			AftIndex+=6;
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public void loadPreInfo(){
		try {
			String preContent = manager.getPreContent(5, preIndex, docName);
			moreInfo = preContent+moreInfo;
			preIndex-=6;
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public String getMoreInfo() {
		return moreInfo;
	}
	public void setMoreInfo(String moreInfo) {
		this.moreInfo = moreInfo;
	}
}
